//
//  MZPickerViewController.m
//  meizhuang
//
//  Created by sky on 16/9/7.
//  Copyright © 2016年 netease. All rights reserved.
//

#import <Masonry/Masonry.h>

#import "WFPickerViewController.h"

@interface WFPickerViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>

@end

@implementation WFPickerViewController

- (void)loadView {
    [super loadView];
    
    _pickerView = [[UIPickerView alloc] init];
    _pickerView.dataSource = self;
    _pickerView.delegate = self;
    if (_selectedIndexPath) {
        [self selectRowsOfComponentsAtIndexPath:_selectedIndexPath];
    }
    [self.contentView addSubview:_pickerView];

    _cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _cancelButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelButton setTitleColor:[UIColor themeColor]
                        forState:UIControlStateNormal];
    [_cancelButton addTarget:self
                      action:@selector(cancelButtonClicked)
            forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_cancelButton];
    
    _okButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _okButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_okButton setTitle:@"确定"
               forState:UIControlStateNormal];
    [_okButton setTitleColor:[UIColor themeColor]
                    forState:UIControlStateNormal];
    [_okButton addTarget:self
                  action:@selector(okButtonClicked)
        forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_okButton];

    [_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.pickerView.superview).insets(UIEdgeInsetsMake(0, 0, 44, 0));
    }];
    [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.cancelButton.superview);
        make.top.equalTo(self.pickerView.mas_bottom);
        make.width.equalTo(self.okButton);
        make.right.equalTo(self.okButton.mas_left);
    }];
    [_okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.okButton.superview);
        make.top.equalTo(self.cancelButton);
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)cancelButtonClicked {
    [self dismissViewControllerAnimated:YES completion:NULL];
    if ([_delegate respondsToSelector:@selector(pickerViewControllerDidCancel:)]) {
        [_delegate pickerViewControllerDidCancel:self];
    }
}

- (void)okButtonClicked {
    NSIndexPath *indexPath = [NSIndexPath new];
    NSUInteger length = self.dataSource.count;
    for (int component = 0; component < length; component ++ ) {
        NSUInteger selectedRow = [_pickerView selectedRowInComponent:component];
        indexPath = [indexPath indexPathByAddingIndex:selectedRow];
    }
    _selectedIndexPath = indexPath;
    [self dismissViewControllerAnimated:YES completion:NULL];
    if ([_delegate respondsToSelector:@selector(pickerViewController:didPickAtIndexPath:)]) {
        [_delegate pickerViewController:self didPickAtIndexPath:indexPath];
    }
}

- (void)setDataSource:(NSArray<NSArray<NSString *> *> *)dataSource {
    if ([[dataSource firstObject] isKindOfClass:[NSString class]]) {
        _dataSource = @[(NSArray <NSString *> *)dataSource];
    }else {
        _dataSource = dataSource;
    }
    [_pickerView reloadAllComponents];
}

- (void)selectRowsOfComponentsAtIndexPath:(NSIndexPath *)indexPath {
    for (int component = 0; component<indexPath.length; component++) {
        NSInteger row = [indexPath indexAtPosition:component];
        [_pickerView selectRow:row inComponent:component animated:YES];
    }
}

#pragma mark - ==================== picker view delegate ================
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return pickerView.width/self.dataSource.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 44;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSArray *rows = self.dataSource[component];
    return rows.count;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.dataSource.count;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    NSArray *rows = self.dataSource[component];
    NSString *title = rows[row];
    
    UILabel *label = nil;
    if ([view isKindOfClass:[UILabel class]]) {
        label = (UILabel *)view;
    }
    if (!label) {
        label = [UILabel new];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = SCFont(16);
        label.textColor = [UIColor textContentColor];
    }
    label.text = title;
    return label;
}

@end
